package com.ketangpai.service.impl;

import com.ketangpai.entity.Student;
import com.ketangpai.entity.User;
import com.ketangpai.entity.response.BaseResponse;
import com.ketangpai.mapper.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author jiahao
 * @date 2024/2/14 20:09
 */
@Service
public class LoginService implements com.ketangpai.service.LoginService {
    @Autowired
    LoginMapper loginMapper;
    @Override
    @Transactional
    public BaseResponse login(int account,String password) {
        BaseResponse<User> userBaseResponse = new BaseResponse<>();
        try {
            User user = loginMapper.getUser(account);
            if (user == null) {
                userBaseResponse.setStatus("failed");
                userBaseResponse.setMessage("no account");
            } else {
                int type = user.getType();
                if (type == 1) {
                    Student student = loginMapper.getStudent(account,password);
                    if (student != null) {
                        userBaseResponse.setStatus("success");
                        userBaseResponse.setMessage("success");
                        userBaseResponse.setData(user);
                    } else {
                        userBaseResponse.setStatus("failed");
                        userBaseResponse.setMessage("password error");
                    }
                } else if (type == 0) {
                    Student teacher = loginMapper.getTeacher(account, password);
                    if (teacher != null) {
                        userBaseResponse.setStatus("success");
                        userBaseResponse.setMessage("success");
                        userBaseResponse.setData(user);
                    } else {
                        userBaseResponse.setStatus("failed");
                        userBaseResponse.setMessage("password error");
                    }
                }
            }
            return userBaseResponse;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
